﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class SalaryDateAddUI : Form
    {
        public SalaryDateAddUI()
        {
            InitializeComponent();

            cmbCmp.Enabled = false;
        }

        private void chkCmp_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCmp.Checked)
            {
                cmbCmp.Enabled = false;
            }
            else
            {
                cmbCmp.Enabled = true;
                String sql = "SELECT CmpId,CmpName FROM Company";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                cmbCmp.DataSource = dt;
                cmbCmp.DisplayMember = "CmpName";
                cmbCmp.ValueMember = "CmpId";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtSalaryDate.Text.Trim().ToString().Length > 0)
            {
                if (chkCmp.Checked)
                {
                    String sql = "SELECT CmpId,CmpName FROM Company";
                    DataTable dtCmp = DBSession.ExecuteDataTable(sql, null);
                    if (dtCmp != null && dtCmp.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtCmp.Rows)
                        {
                            sql = "INSERT INTO SalaryDate(SalaryDate,CmpId,CreateDate) VALUES('" + txtSalaryDate.Text.Trim().ToString() + "','" + dr["CmpId"] + "','"+DateTime.Now.ToString()+"')";
                            DBSession.ExecuteNonQuery(sql, null);
                        }
                        MessageBox.Show("成功为所有企业创建薪资月份：" + txtSalaryDate.Text.Trim().ToString() +"!");
                    }
                    else
                    {
                        MessageBox.Show("找不到企业ID号！");
                    }
                }
                else
                {
                    String sql = "INSERT INTO SalaryDate(SalaryDate,CmpId,CreateDate) VALUES('" + txtSalaryDate.Text.Trim().ToString() + "','" + cmbCmp.SelectedValue + "','" + DateTime.Now.ToString() + "')";
                    DBSession.ExecuteNonQuery(sql, null);
                    MessageBox.Show("成功为["+cmbCmp.SelectedText+"]创建薪资月份：" + txtSalaryDate.Text.Trim().ToString() + "!");
                }
            }
            else
            {
                MessageBox.Show("薪资月份不能为空！");
            }
        }
    }
}
